TOOL.Category = "NPC Control"
TOOL.Name = "NPC Spawner"
TOOL.Command = nil
TOOL.ConfigName = ""

local NPCData = {}
NPCData.base = { spawnflags = 516, physdamagescale = 1.0 }

NPCData.antlion_worker = { spawnflags = 262660, realclass = "antlion" }

NPCData.citizen = { spawnflags = 262660, citizentype = 0 }
NPCData.rebel = { spawnflags = 262660, citizentype = 3, realclass = "citizen" }
NPCData.downtrodden = { spawnflags = 262660, citizentype = 1, realclass = "citizen" }
NPCData.refugee = { spawnflags = 262660, citizentype = 2, realclass = "citizen" }

NPCData.c_soldier = { spawnflags = 516, model = "models/combine_soldier.mdl", realclass = "combine_s" }
NPCData.c_prisong = { spawnflags = 516, model = "models/combine_soldier_prisonguard.mdl", realclass = "combine_s" }
NPCData.c_elite = { spawnflags = 516, model = "models/combine_super_soldier.mdl", realclass = "combine_s" }

NPCData.turret_floor = { spawnflags = 32, SkinNumber = 0 }
//Skin 2 is so much better; Skin 1 is availiable
NPCData.friendlyturret = { spawnflags = 544, SkinNumber = 0, realclass = "turret_floor", checkforpermission = 1 }
NPCData.turret_ceiling = { spawnflags = 32, checkforpermission = 1 }

if CLIENT then
  language.Add( "Tool_npc_spawner_gun_name", "NPC Spawner" )
  language.Add( "Tool_npc_spawner_gun_desc", "Spawn or kill the NPC of your choice" )
  language.Add( "Tool_npc_spawner_gun_0", "Left click to spawn the NPC of your choice, Right-click to slay it" )
  language.Add( "Tool_npc_spawner_gun_notnpc", "That is not an NPC!" )
end

TOOL.ClientConVar[ "npc" ] = "npc_gman"
TOOL.ClientConVar[ "removecorpse" ] = "0"
TOOL.ClientConVar[ "npcwep" ] = ""
TOOL.ClientConVar[ "spawnangle" ] = "180"

TOOL.ClientConVar[ "autoselect_move" ] = "1"
TOOL.ClientConVar[ "autoselect_attack" ] = "1"

local function npc_doNotify( message, symbol, user )
  if SERVER then
    user:SendLua("GAMEMODE:AddNotify(\"" .. message .. "\", " .. symbol .. ", \"5\"); surface.PlaySound( \"ambient/water/drip" .. math.random(1, 4) .. ".wav\" )")
  end
end


//Yoink!
//generic
//error
//undo
//hint
//cleanup

local function LimitCheck( ply )
  // Always allow in single player
	if (SinglePlayer()) then return true end
  
  local c = GetConVarNumber("sbox_maxnpcs")
  
	if ( ply:GetCount( "npcs" ) < c || c < 0 ) then
    return true
	else
    npc_doNotify( "You've hit the NPCs limit!", "NOTIFY_ERROR", ply )
    return false
	end
end

//TraceRes
function TOOL:LeftClick( trace )
  local ply = self:GetOwner()
  local tospawn = self:GetClientInfo("npc")
  
  if (tospawn == nil) then
    npc_doNotify( "Error spawning entity", "NOTIFY_ERROR", ply )
  end
  
  if (trace.Hit && trace.HitWorld && !trace.HitSky) then 
    if SERVER then
      //Spawn!!
      local s = string.Replace( tospawn, "npc_", "" )
      local spawnData = {}
      if (NPCData[s] == nil) then
        spawnData = NPCData.base
      else
        spawnData = NPCData[s]
      end
      if (spawnData.checkforpermission != nil && spawnData.checkforpermission == 1) then
        if (GetConVarNumber("npc_control_allow_" .. s) == 0) then
          if (!SinglePlayer()) then
            npc_doNotify("You are not allowed to spawn that NPC", "NOTIFY_ERROR", ply)
            return true
          end
        end
      end
      local entClass = ""
      if (spawnData.realclass != nil) then
        tospawn = "npc_" .. spawnData.realclass
      end
      if (LimitCheck( ply )) then
        local newEnt = ents.Create(tospawn)
        if (spawnData != NPCData.base) then
          for k, v in pairs(NPCData.base) do
            newEnt:SetKeyValue(k, tostring(v))
          end
        end

        for k, v in pairs(spawnData) do
          if (k != "realclass" && k != "checkforpermission") then
            newEnt:SetKeyValue(k, tostring(v))
          end
        end
        if (self:GetClientInfo("npcwep") != "") then
          newEnt:SetKeyValue("additionalequipment", self:GetClientInfo("npcwep"))
        end
        newEnt:SetPos(trace.HitPos + Vector(0, 0, 1)) //This stops NPCs from spawning with their knee's perma-bent.
        newEnt:SetAngles(Vector(0, self:GetClientNumber("spawnangle"), 0))
        newEnt:Spawn()
        if (self:GetClientNumber("autoselect_move") == 1) then
          mover_selectedNpcs[newEnt] = newEnt
        end
        if (self:GetClientNumber("autoselect_attack") == 1) then
          selectedNpcs_relations[newEnt] = newEnt
        end
        undo.Create("npc")
          undo.AddEntity(newEnt)
          undo.SetPlayer(ply)
        undo.Finish()
        ply:AddCount( "npcs", newEnt )
        cleanup.Add( ply, "npcs", newEnt )
      end
    end
    return true
  end
  return false
end 
  
function TOOL:RightClick( trace )
  if (trace.Hit && trace.HitNonWorld) then
    if (trace.Entity:IsNPC()) then
      //What if it's invunurable?
      local entclass = trace.Entity:GetClass()
      if (entclass == "npc_turret_floor" || entclass == "npc_rollermine") then
        trace.Entity:Remove()
        return true
      end
      
      if (self:GetClientNumber("removecorpse", 0) == 1) then
        trace.Entity:Remove()
      else
        if SERVER then
          trace.Entity:TakeDamage(trace.Entity:Health(), self:GetOwner())
        end
      end
    else
      npc_doNotify("#Tool_npc_spawner_gun_notnpc", "NOTIFY_ERROR", self:GetOwner())
      return false
    end
    return true
  end
  return false
end 

function TOOL.BuildCPanel(panel)
  
  
  
  panel:AddControl("Header", { Text = "NPC Spawner", Description = "Used to spawn the NPC of your choice" })
  //panel:AddControl("TextBox", { Label = "NPC:", Description = "What NPC is going to spawn? (Don't forget the npc_)", MaxLength = 50, Text = "gman", Command = "npc_spawner_gun", WaitForEnter = true })
  //panel:AddControl("TextBox", { Label = "Weapon:", Description = "What weapon is that NPC going to have? (Don't forget the weapon_)", MaxLength = 50, Text = "Weapon", Command = "npc_spawner_gun_npcwep", WaitForEnter = true })
  
  panel:AddControl("Slider", { Label = "Spawn Angle", min = 0, max = 360, Command = "npc_spawner_gun_spawnangle" })
  
  //I wish these were alphabetical :( I wasted all my caps on nothing
  local data = {}
  data.Label = "NPC:"
  data.MenuButton = 0
  data.Height = 120
  data.Options = {}
  data.Options["Alyx Vance"] = {npc_spawner_gun_npc = "npc_alyx"}
  data.Options["Barney"] = {npc_spawner_gun_npc = "npc_barney"}
  data.Options["Wallace Breen"] = {npc_spawner_gun_npc = "npc_breen"}
  data.Options["Dog"] = {npc_spawner_gun_npc = "npc_dog"}
  data.Options["Eli Vance"] = {npc_spawner_gun_npc = "npc_eli"}
  data.Options["G-Man"] = {npc_spawner_gun_npc = "npc_gman"}
  data.Options["Dr. Kleiner"] = {npc_spawner_gun_npc = "npc_kleiner"}
  data.Options["Mossman"] = {npc_spawner_gun_npc = "npc_mossman"}
  data.Options["Vortigaunt"] = {npc_spawner_gun_npc = "npc_vortigaunt"}
  
  data.Options["Citizen"] = {npc_spawner_gun_npc = "npc_citizen"}
  data.Options["Rebel"] = {npc_spawner_gun_npc = "npc_rebel"}
  data.Options["Refugee"] = {npc_spawner_gun_npc = "npc_refugee"}
  data.Options["Downtrodden Citizen"] = {npc_spawner_gun_npc = "npc_downtrodden"}
  
  data.Options["Father Grigori"] = {npc_spawner_gun_npc = "npc_monk"}
  data.Options["Zombie"] = {npc_spawner_gun_npc = "npc_zombie"}
  data.Options["Zombine"] = {npc_spawner_gun_npc = "npc_zombine"}
  data.Options["Zombie Torso"] = {npc_spawner_gun_npc = "npc_zombie_torso"}
  data.Options["Poison Zombie"] = {npc_spawner_gun_npc = "npc_poisonzombie"}
  data.Options["Fast Zombie"] = {npc_spawner_gun_npc = "npc_fastzombie"}
  data.Options["Fast Zombie Torso"] = {npc_spawner_gun_npc = "npc_fastzombie_torso"}
  data.Options["Headcrab"] = {npc_spawner_gun_npc = "npc_headcrab"}
  data.Options["Headcrab Fast"] = {npc_spawner_gun_npc = "npc_headcrab_fast"}
  data.Options["Headcrab Black"] = {npc_spawner_gun_npc = "npc_headcrab_black"}
  data.Options["Antlion"] = {npc_spawner_gun_npc = "npc_antlion"}
  data.Options["Antlion Guard"] = {npc_spawner_gun_npc = "npc_antlionguard"}
  data.Options["Antlion Worker"] = {npc_spawner_gun_npc = "npc_antlion_worker"}
  data.Options["Barnacle"] = {npc_spawner_gun_npc = "npc_barnacle"}
  data.Options["Crow"] = {npc_spawner_gun_npc = "npc_crow"}
  data.Options["Pigeon"] = {npc_spawner_gun_npc = "npc_pigeon"}
  data.Options["Seagull"] = {npc_spawner_gun_npc = "npc_seagull"}
  
  data.Options["Metro Police"] = {npc_spawner_gun_npc = "npc_metropolice"}
  data.Options["Combine Soldier"] = {npc_spawner_gun_npc = "npc_c_soldier"}
  data.Options["Combine PrisonGuard"] = {npc_spawner_gun_npc = "npc_c_prisong"}
  data.Options["Combine Elite"] = {npc_spawner_gun_npc = "npc_c_elite"}
  data.Options["Hunter"] ={npc_spawner_gun_npc = "npc_hunter"}
  
  data.Options["City Scanner"] = {npc_spawner_gun_npc = "npc_cscanner"}
  data.Options["Manhack"] = {npc_spawner_gun_npc = "npc_manhack"}
  data.Options["Turret"] = {npc_spawner_gun_npc = "npc_turret_floor"}
  data.Options["Friendly Turret"] = {npc_spawner_gun_npc = "npc_friendlyturret"}
  data.Options["Ceiling Turret"] = {npc_spawner_gun_npc = "npc_turret_ceiling"}
  data.Options["Rollermine"] = {npc_spawner_gun_npc = "npc_rollermine"}
  
  panel:AddControl("ListBox", data)
  
  data = {}
  data.Label = "Weapon:"
  data.MenuButton = 0
  data.Options = {}
  data.Options["None"] = {npc_spawner_gun_npcwep = ""}
  data.Options["Shotgun"] = {npc_spawner_gun_npcwep = "weapon_shotgun"}
  data.Options["Pistol"] = {npc_spawner_gun_npcwep = "weapon_pistol"}
  data.Options["SMG"] = {npc_spawner_gun_npcwep = "weapon_smg1"}
  data.Options["AR2"] = {npc_spawner_gun_npcwep = "weapon_ar2"}
  data.Options["Crowbar"] = {npc_spawner_gun_npcwep = "weapon_crowbar"}
  data.Options["RPG"] = {npc_spawner_gun_npcwep = "weapon_rpg"}
  data.Options[".357"] = {npc_spawner_gun_npcwep = "weapon_357"}
  data.Options["Annabelle"] = {npc_spawner_gun_npcwep = "weapon_annabelle"}
  data.Options["Stunstick"] = {npc_spawner_gun_npcwep = "weapon_stunstick"}
  panel:AddControl("ComboBox", data)
  
  panel:AddControl("CheckBox", { Label = "Remove corpse on slay", Description = "Remove the ragdoll when you slay an NPC with this weapon", Command = "npc_spawner_gun_removecorpse" })
  panel:AddControl("CheckBox", { Label = "Auto-select for movement", Command = "npc_spawner_gun_autoselect_move" })
  panel:AddControl("CheckBox", { Label = "Auto-select for attacking", Command = "npc_spawner_gun_autoselect_attack" })
end
